<?php
	function cleanText($intext)
	{
		//return utf8_encode(htmlspecialchars(stripslashes($intext)));
		return htmlspecialchars($intext);
	}
	
	function buildrss ($dir)
	{
		require_once($dir.'config.php');
		//mysql_connect($DBHOST, $DBUSER, $DBPASS) or die(mysql_error());
		//mysql_select_db($DBNAME) or die(mysql_error());
		$filename = $dir."feed2.rss";
		$file = fopen($filename, "w");
		
		// informations basique RSS 2.0
		fwrite($file, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
		fwrite($file, "<rss version=\"2.0\">\n");
		fwrite($file, "<channel>\n");
		fwrite($file, "<title>Hurmpf...</title>\n");
		fwrite($file, "<link>http://hurmpf.free.fr</link>\n");
		fwrite($file, "<description>Hurmpf...</description>\n");
		fwrite($file, "<language>fr-FR</language>\n");
		fwrite($file, "<managingEditor>hurmpf@free.fr</managingEditor>\n");
		fwrite($file, "<webMaster>hurmpf@free.fr</webMaster>\n");
		
		// posts
		$sql = 'SELECT * FROM posts ORDER BY date DESC LIMIT 10';
		$res = mysql_query($sql) or die('Erreur SQL !<br>'.$sql.'<br>'.mysql_error());
		$data = array();
		while(@$row = mysql_fetch_array($res))
		{
			$id = $row['id'];
			$title = cleanText($row['titre']);
			$link = "http://hurmpf.free.fr/?p=".$row["id"];
			$description = str_replace("&quot;img/","&quot;http://hurmpf.free.fr/img/",cleanText($row["texte"]));
			$description .= '<img src="http://hurmpf.fr/stats/record.php?id=hurmpf&amp;ref=Flux%20RSS&amp;url='.urlencode($link).'"/>'; // stats
			$date = strtotime($row['date']);
			$data["$date"] = array($title,$link,$description);
		}
		// commentaires
		$sql = 'SELECT * FROM comments ORDER BY date DESC LIMIT 10';
		$res = mysql_query($sql) or die('Erreur SQL !<br>'.$sql.'<br>'.mysql_error());
		while(@$row = mysql_fetch_array($res))
		{
			$title = cleanText("Commentaire de ".$row['nom']);
			$link = "http://hurmpf.free.fr/?p=".$row["ref"]."#".$row["id"];
			$description = cleanText($row["texte"]);
			$description .= '<img src="http://hurmpf.fr/stats/record.php?id=hurmpf&amp;ref=Flux%20RSS&amp;url='.urlencode($link).'"/>'; // stats
			$date = strtotime($row['date']);
			$data["$date"] = array($title,$link,$description);
		}
		krsort($data);
		// output
		$i = 0;
		foreach ($data as $d => $item)
		{
			if($i++==10) break;
			$pubDate = date('r',$d);
			fwrite($file, "<item>\n");
			fwrite($file, "<title>$item[0]</title>\n");
			fwrite($file, "<link>$item[1]</link>\n");
			fwrite($file, "<guid isPermaLink=\"true\">$item[1]</guid>\n");
			fwrite($file, "<author>hurmpf@free.fr</author>\n");
			fwrite($file, "<description>$item[2]</description>\n");
			fwrite($file, "<pubDate>$pubDate</pubDate>\n");
			fwrite($file, "</item>\n");
		}
		fwrite($file, "</channel>\n");
		fwrite($file, "</rss>\n");
		fclose($file);
	}
?>